package com.csd.serviceImpl;

import java.util.Iterator;
import java.util.List;

import com.csd.dao.ProjectTypeDAO;
import com.csd.exception.*;
import com.csd.pojo.ProjectType;
import com.csd.service.IProjectTypeService;
/**
 * 
 * Service：工程类型
 * @author csd
 * @version 1.0
 * 
 * <br> 修订时间：2009-12-17
 * <br> 修改原因：创建文件
 * <br> 修改人：何文深
 * 
 * <br> 
 * 
 */
public class ProjectTypeService implements IProjectTypeService {

	private ProjectTypeDAO projectTypeDAO;

	public ProjectTypeDAO getProjectTypeDAO() {
		return projectTypeDAO;
	}

	public void setProjectTypeDAO(ProjectTypeDAO projectTypeDAO) {
		this.projectTypeDAO = projectTypeDAO;
	}

	/**
	 * 列出所有项目类别
	 * @param 
	 * @return List<ProjectType>
	 */
	public List<ProjectType> listAllProjectType() {
		List<ProjectType> ptList = null;
		try {
			ptList = projectTypeDAO.findAll();

		} catch (RuntimeException re) {
			throw new GetException(re, "列出所有工程类型异常");
		}
		return ptList;
	}

	/**
	 * 添加项目类别
	 * 
	 * @param projectType
	 *            项目类别实例
	 * @return 添加成功为true，否则为false
	 */
	public boolean addProjectType(ProjectType projectType) {
		boolean bl = false;
		try {
			projectType.setEnable(false);
			projectTypeDAO.save(projectType);
			bl = true;

		} catch (RuntimeException re) {
			throw new SaveException(re, "添加工程类型异常");
		}
		return bl;
	}

	/**
	 * 删除项目类别实例
	 * 
	 * @param projectTypeID
	 *            项目类别id
	 * @return 删除成功为true，否则为false
	 */
	public boolean deleteProjectType(Integer projectTypeID) {
		ProjectType pType = new ProjectType();
		boolean bl = false;
		try {
			pType.setId(projectTypeID);
			projectTypeDAO.delete(pType);
			bl = true;
		} catch (RuntimeException re) {
			throw new DeleteException(re, "删除工程类型异常");
		}
		return bl;
	}

	/**
	 * 对项目类别状态进行改变，启动或关闭
	 * 
	 * @param isEnable
	 *            启动或关闭动作
	 * @return 修改成功为true，否则为false
	 */
	public boolean changeProjectType(Integer projectTypeID, boolean isEnable) {
		boolean bl = false;
		ProjectType projectType = null;
		try {
			projectType = projectTypeDAO.findById(projectTypeID);
			if (projectType != null) {
				projectType.setEnable(isEnable);
			}
			projectTypeDAO.update(projectType);
			bl = true;
		} catch (RuntimeException re) {
			throw new UpdateException(re, "启动/关闭工程类型异常");
		}
		return bl;
	}

	/**
	 * 列表启动的项目类别
	 * 
	 * @return 项目类别实例列表
	 */
	public List<ProjectType> listEnableProjectType() {
		List<ProjectType> ptList = null;
		try {
			ptList = projectTypeDAO.findByEnable(true);

		} catch (RuntimeException re) {
			throw new GetException(re, "列出已启动工程类型异常");
		}
		return ptList;
	}
	
	/**
	 * 根据ID查询工程类型
	 * 
	 * @return 项目类别实例
	 */
	public ProjectType getProjectTypeByID(Integer ID) {
		ProjectType projectType = null;

		try {
			projectType = projectTypeDAO.findById(ID);

		} catch (RuntimeException re) {
			throw new GetException(re, "查询工程类型异常");
		}

		return projectType;
	}
	
	public int getProjectTypeMaxMemberByID(Integer ID)
	{
		int max = projectTypeDAO.getProjectTypeMaxMemberByID(ID);
		return max;
	}
	
	public int getProjectTypeMaxTeacherByID(Integer ID)
	{
		return projectTypeDAO.getProjectTypeTeacherMemberByID(ID);
	}

}
